//
//  GeneralAuditCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/3/8.
//  Copyright © 2022 zwb. All rights reserved.
//

import UIKit
enum ZyaConTypeEnum {
    // 内容类型: 纯内容  分割线  文件类型  报表类型  带下划线跳转的文字
    case content,seperate,file,report,underLine
}
class ZyaGeneralAuditCell: UITableViewCell {
    
    private lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 3
        return v
    }()
    private  lazy var titleLab:UILabel = {
        let v = UILabel()
        v.textColor = UIColor(kshex:"888888")
        v.numberOfLines = 0
        return v
    }()
    private lazy var contentLab:UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
        v.textAlignment = .right
        return v
    }()
    
    private lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#e5e5e5")
        return v
    }()
    private lazy var icon:UIImageView = {
        let v = UIImageView()
        return v
    }()
    
    var conType:ZyaConTypeEnum = .content {
        didSet {
            self.titleLab.text = titlex
            
            
            
            if contentx == "" {
                self.contentLab.text = contentx
                titleLab.snp.remakeConstraints { make in
                    make.left.equalTo(bg).offset(8)
                    make.centerY.equalTo(bg)
                    make.right.equalTo(bg).offset(-25 + 3)
                    make.bottom.equalTo(bg).offset(-6)
                }
                
            } else {
                
                if contentx.contains("<span") || contentx.contains("<p") {
                    do {
                        if let data = contentx.data(using: String.Encoding.unicode, allowLossyConversion: true) {
                            let attStr = try NSAttributedString.init(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,], documentAttributes: nil)
                            self.contentLab.attributedText = attStr
                        }
                    } catch {
                        self.contentLab.attributedText = nil
                         self.contentLab.text = contentx
                    }
                } else {
                    self.contentLab.attributedText = nil
                     self.contentLab.text = contentx
                }
                 
                
                titleLab.snp.remakeConstraints { make in
                    make.left.equalTo(bg).offset(8)
                    make.centerY.equalTo(bg)
                    make.width.equalTo(80)
                }
                
                contentLab.snp.remakeConstraints { make in
                    make.right.equalTo(bg).offset(-15)
                    make.top.equalTo(bg).offset(6)
                    make.left.equalTo(titleLab.snp.right)
                    make.height.equalTo(ItemHHeight).priority(666)
                    make.bottom.equalTo(bg).offset(-6)
                }
            }
            
            
            

            // 标题处理颜色
            switch conType {
            case .content:
                icon.isHidden = true
                contentLab.textColor = .black
            case .file:
                icon.isHidden = false
                 contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                if contentx.contains("pdf") {
                    icon.image = UIImage(named: "icon_pdf")
                } else if contentx.contains("doc") {
                    icon.image = UIImage(named: "icon_word")
                } else if contentx.contains("xls") {
                    icon.image = UIImage(named: "icon_excel")
                } else {
                    icon.image = UIImage(named: "icon_pic")
                }
             case .report:
                icon.isHidden = false
                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                icon.image = UIImage(named: "icon_report")
            case .underLine:
                icon.isHidden = true
                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                contentLab.underline()
                
            default:
                break
            }
            
           
     
 
        }
    }
    var titlex = ""
    var contentx = ""
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = UIColor.clear
        bg.backgroundColor = .white
        self.contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(6)
            make.top.equalTo(self.contentView)
            make.right.equalTo(self.contentView).offset(-6)
            make.bottom.equalTo(self.contentView)
        }
        bg.addSubview(titleLab)
        bg.addSubview(contentLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.centerY.equalTo(bg)
            make.width.equalTo(80)
        }
        
        contentLab.snp.makeConstraints { make in
            make.right.equalTo(bg).offset(15)
            make.top.equalTo(bg).offset(6)
            make.left.equalTo(titleLab.snp.right)
            make.height.equalTo(ItemHHeight).priority(666)
            make.bottom.equalTo(bg).offset(-6)
        }
        bg.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.right.equalTo(bg).offset(-8)
            make.height.equalTo(1)
            make.bottom.equalTo(bg).offset(0.5)
        }
        bg.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.width.equalTo(25)
            make.height.equalTo(25)
            make.centerY.equalTo(bg)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
    
    
}
